/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.ProcurementLnCtlr;
import org.gov.vis.data.controller.ProcurementLnCtlrImpl;
import org.gov.vis.data.dto.ProcurementLNDto;
import org.gov.vis.data.gui.ProcurementLNDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author user
 *
 */
public class ProcurementLNListener implements ActionListener,BaseListener,FocusListener {



	private ProcurementLNDialog procurementLnDailog;
	private ProcurementLnCtlr procurementLnCtlr = new ProcurementLnCtlrImpl();
	private ErrorsDisplayJPanel errorMsgPanel;
	final private ResourceBundle rb = Util.getResources();
	private ProcurementLNDto procurementLnDto;

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public ProcurementLNListener(ProcurementLNDialog procurementLnsDialog){

		this.procurementLnDailog = procurementLnsDialog;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		errorMsgPanel = new ErrorsDisplayJPanel(procurementLnDailog.getjPanel1());
		errorMsgPanel.clearMsgPanel();
		
		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_save"))
				|| actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){
			//Method called to either Add or Edit the GopalaMithra details
			addOrEditProcurementLN();
		}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_view"))){			
			//Method called to view the details of ProcurementLN Object based on the passed procurementLnId
			viewProcurementLN(1);
		}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
			//Method called to delete the ProcurementLN Object based on the passed procurementLnId.
			deleteProcurementLN(1);
			}else if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_reset"))){
			//Method called to clear the screen data
			clear();
		}
	}//end actionPerformed


	private void viewProcurementLN(final long  _procurementLNId) {
		// TODO Auto-generated method stub
		try{
			final ProcurementLNDto _procurementLNDto = procurementLnCtlr.getPrLN(_procurementLNId);
			setDtoDetailsToGui(_procurementLNDto);
			procurementLnDailog.validate();
			procurementLnDailog.repaint();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * Method called to delete the ProcurementLN Object based on the passed procurementLnId
	 * @param _procurementLnId
	 */
	private void deleteProcurementLN(final long  _procurementLnId){
		try{
			procurementLnCtlr.deletePrLN(_procurementLnId);
		}catch(Exception e){
			e.getMessage();
		}
	}//end deleteProcurementLN
	
	/**
	 * Method called to either Add or Edit the GopalaMithra details
	 */
	private void addOrEditProcurementLN(){
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(procurementLnDailog);

		try{
			//Method called to Add or Edit the GopalaMithra
			procurementLnCtlr.addOrEditPrLN(procurementLnDto);

			 /*List<String> successMsgLst = new ArrayList<String>();
			 successMsgLst.add(rb.getString(GuiConstants.DATA_ADD_DISTRBUTIONSEMEN_SUCCESS));
			 errorMsgPanel.setSuccessMessages(successMsgLst);
			 */
			 JOptionPane.showMessageDialog(procurementLnDailog, rb.getString(GuiConstants.DATA_ADD_PROCUREMENTLN_SUCCESS));
			// procurementLnDailog.dispose();
				
			
			//Method called to clear the screen data
			clear();
		}catch(Exception e){
			 List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = ProcurementLnCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_ln_code").toUpperCase())){
						procurementLnDailog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_ln_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_ln_dtofrcpt").toUpperCase())){
						procurementLnDailog.getLblDateOfReceipt().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_ln_dtofrcpt").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_ln_frmstation").toUpperCase())){
						procurementLnDailog.getLblFromStation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_ln_frmstation").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_ln_vehicleno").toUpperCase())){
						procurementLnDailog.getLblVehicalNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_ln_vehicleno").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase())){
						procurementLnDailog.getLblKgs().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase());
						errorMsgLst.add(errMsg);
					}
										
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		
	}//end addOrEditGopalaMithra

	/**
	 * Method called to clear the screen data
	 */
	public void clear() {
		// TODO Auto-generated method stub

		//set the Field's data to empty values
		procurementLnDailog.getTxtFldCode().setText("");
		procurementLnDailog.getTxtFldLNConvertedLtrs().setText("");
		procurementLnDailog.getDtChoDateOfReceipt().setDate(null);
		procurementLnDailog.getTxtFldLNFromStation().setText("");
		procurementLnDailog.getTxtFldLNKgs().setText("");
		procurementLnDailog.getTxtFldLNVehicalNo().setText("");
		procurementLnDailog.getTxtFldLNEvoporationLoss().setText("");
		
		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);


	}//end clear


	/**
	 * Method called to read the data from DTO and set them to GUI.
	 * @param _guiObject
	 */
	@Override
	public void setDtoDetailsToGui(Object object) {
		// TODO Auto-generated method stub
		final ProcurementLNDto _procurementLNDto = (ProcurementLNDto)object;
		procurementLnDailog.getTxtFldLNConvertedLtrs().setText(_procurementLNDto.getCnvrtLtrs());
		procurementLnDailog.getTxtFldDOReceipt().setText(DU.format(_procurementLNDto.getDoRpt()));
		procurementLnDailog.getTxtFldLNFromStation().setText(_procurementLNDto.getFrmStation());
		procurementLnDailog.getTxtFldLNKgs().setText(_procurementLNDto.getKgs());
		procurementLnDailog.getTxtFldLNVehicalNo().setText(_procurementLNDto.getVehicleNo());
		procurementLnDailog.getTxtFldLNEvoporationLoss().setText(_procurementLNDto.getEvoprLoss());

	}

	/**
	 * Method called to read the data from GUI and set them to DTO Object 
	 * @param _guiObject
	 */
	@Override
	public void setGuiDetailsToDto(Object object) {
		// TODO Auto-generated method stub
		final ProcurementLNDialog _procurementLNDialog = (ProcurementLNDialog)object;
		procurementLnDto = new ProcurementLNDto();
		procurementLnDto.setCode(_procurementLNDialog.getTxtFldCode().getText());
		procurementLnDto.setCnvrtLtrs(_procurementLNDialog.getTxtFldLNConvertedLtrs().getText());
		if(_procurementLNDialog.getDtChoDateOfReceipt().getDate() != null){
			procurementLnDto.setDoRpt(_procurementLNDialog.getDtChoDateOfReceipt().getDate());
			}
		procurementLnDto.setFrmStation(_procurementLNDialog.getTxtFldLNFromStation().getText());
		procurementLnDto.setKgs(_procurementLNDialog.getTxtFldLNKgs().getText());
		procurementLnDto.setVehicleNo(_procurementLNDialog.getTxtFldLNVehicalNo().getText());
		procurementLnDto.setEvoprLoss(_procurementLNDialog.getTxtFldLNEvoporationLoss().getText());


	}

	public void setProcurementLNsDailog(final ProcurementLNDialog _distrbutionLNDailog) {
		this.procurementLnDailog = _distrbutionLNDailog;
	}

	public ProcurementLNDialog getGopalaMithrasDailog() {
		return procurementLnDailog;
	}

	@Override
	public void clearGUILabelColors(Color _lblColor) {
		//set the Field's Labels to default Black color
		procurementLnDailog.getLblCode().setForeground(_lblColor);
		procurementLnDailog.getLblConvertedLtrs().setForeground(_lblColor);
		procurementLnDailog.getLblEvoporation().setForeground(_lblColor);
		procurementLnDailog.getLblFromStation().setForeground(_lblColor);
		procurementLnDailog.getLblKgs().setForeground(_lblColor);
		procurementLnDailog.getLblVehicalNo().setForeground(_lblColor);
		procurementLnDailog.getLblDateOfReceipt().setForeground(_lblColor);

		
	}

	@Override
	public void focusGained(FocusEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void focusLost(FocusEvent arg0) {
		// TODO Auto-generated method stub
		errorMsgPanel = new ErrorsDisplayJPanel(procurementLnDailog.getjPanel1());
		errorMsgPanel.clearMsgPanel();

		clearGUILabelColors(blackColor);
		
		convertKgsToLitters();

	}

	private void convertKgsToLitters() {
		// TODO Auto-generated method stub
		HashMap<String, String> errorMsgs = new HashMap<String, String>();
		List<String> errorMsgLst = new ArrayList<String>();
		try{
			String _prLnKGErr = ValidatorUtils.validateDecimal(rb.getString("dialog_label_prmt_ln_kgs"), procurementLnDailog.getTxtFldLNKgs().getText(), ValidatorUtils.DECIMALS, 0, Double.MAX_VALUE, 0, 2, false, false);
			if(_prLnKGErr != null)
				errorMsgs.put(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase(), _prLnKGErr);
			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();
			double kgs = Double.parseDouble(procurementLnDailog.getTxtFldLNKgs().getText());
			double litters = kgs * 1.078;
			procurementLnDailog.getTxtFldLNConvertedLtrs().setText(String.valueOf(litters));


		}catch(InvalidInputException iie){
			String errMsg = null;
			if(errorMsgs.containsKey(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase())){
				procurementLnDailog.getLblKgs().setForeground(redColor);
				errMsg = errorMsgs.remove(rb.getString("dialog_label_prmt_ln_kgs").toUpperCase());
				errorMsgLst.add(errMsg);
			}

			errorMsgPanel.setErrorMessages(errorMsgLst);

		}

	}

}//end class
